package cn.qdhlm.collectservice.core.component.command;

import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.common.util.ComUtil;
import cn.qdhlm.collectservice.model.dto.CommandParamDTO;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.log4j.Log4j2;

import java.math.BigDecimal;

/**
 * @Name: CmdAfn03
 * @Description: 设置命令
 * @Author: jiangcs
 */
@Log4j2
public class CmdAfn03 {
    /**
     * 服务器1信息
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f1(int sn, CommandParamDTO commandParamDTO) {
        String ip = commandParamDTO.getIp();
        Integer port = commandParamDTO.getPort();
        if (ComUtil.isEmpty(ip) || ComUtil.isEmpty(port)) {
            log.error("f1: ip or port is null");
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(36);
        // 数据单元标识：FN
        byteBuf.writeByte(0x01);
        // 数据包序号
        byteBuf.writeByte(sn);
        // ip或域名
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(ip)));
        for (int i = 0, len = Constant.BYTE_LENGTH_IP - ip.length(); i < len; i++) {
            byteBuf.writeByte(0x00);
        }
        // 端口
        byteBuf.writeShortLE(port);
        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * 校时
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f3(int sn, CommandParamDTO commandParamDTO) {
        String currentTime = commandParamDTO.getCurrentTime();
        if (ComUtil.isEmpty(currentTime)) {
            log.error("f3: currentTime is null");
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(9);
        // 数据单元标识：FN
        byteBuf.writeByte(0x03);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 时间:ssmmHHddMMyyyy
        String dateTimeReverse = new StringBuffer(currentTime).reverse().toString();
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(dateTimeReverse));
        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * 上传模式
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f4(int sn, CommandParamDTO commandParamDTO) {
        Integer uploadMode = commandParamDTO.getUploadMode();
        if (ComUtil.isEmpty(uploadMode)) {
            log.error("f4: uploadMode is null");
            return null;
        }
        String startTime = commandParamDTO.getStartTime();
        String endTime = commandParamDTO.getEndTime();
        if (uploadMode.compareTo(1) == 0 && (ComUtil.isEmpty(startTime) || ComUtil.isEmpty(endTime))) {
            log.error("f4: uploadMode[{}], startTime[{}] or endTime[{}] is null", uploadMode, startTime, endTime);
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(7);
        // 数据单元标识：FN
        byteBuf.writeByte(0x04);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 上传模式 0：采后传  1：时段传
        byteBuf.writeByte(uploadMode);
        // 开始时间:HHmm
        if (!ComUtil.isEmpty(startTime)) {
            startTime = startTime.replace(":", "");
            byteBuf.writeBytes(CodeUtil.hexStringToByteArray(startTime));
        } else {
            byteBuf.writeShortLE(0x0000);
        }
        // 结束时间:HHmm
        if (!ComUtil.isEmpty(endTime)) {
            endTime = endTime.replace(":", "");
            byteBuf.writeBytes(CodeUtil.hexStringToByteArray(endTime));
        } else {
            byteBuf.writeShortLE(0x0000);
        }
        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * 采集模式
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f5(int sn, CommandParamDTO commandParamDTO) {
        Integer collectMode = commandParamDTO.getCollectMode();
        if (ComUtil.isEmpty(collectMode)) {
            log.error("f5: collectMode is null");
            return null;
        }
        Integer collectCycle = commandParamDTO.getCollectCycle();
        if (collectMode.compareTo(0) == 0 && ComUtil.isEmpty(collectCycle)) {
            log.error("f5: collectMode[{}], collectCycle[{}] is null", collectMode, collectCycle);
            return null;
        }
        String timing1 = commandParamDTO.getTiming1();
        String timing2 = commandParamDTO.getTiming2();
        if (collectMode.compareTo(1) == 0 && (ComUtil.isEmpty(timing1) || ComUtil.isEmpty(timing2))) {
            log.error("f5: collectMode[{}], timing1[{}] or timing2[{}] is null", collectMode, timing1, timing2);
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(9);
        // 数据单元标识：FN
        byteBuf.writeByte(0x05);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 采集模式 0：周期  1：定时
        byteBuf.writeByte(collectMode);
        // 采集间隔
        if (!ComUtil.isEmpty(collectCycle)) {
            byteBuf.writeShortLE(collectCycle);
        } else {
            byteBuf.writeShortLE(0x0000);
        }
        // 定时时间1:mmHH
        if (!ComUtil.isEmpty(timing1)) {
            timing1 = timing1.replace(":", "");
            byteBuf.writeBytes(CodeUtil.hexStringToByteArray(timing1));
        } else {
            byteBuf.writeShortLE(0x0000);
        }
        // 定时时间2:mmHH
        if (!ComUtil.isEmpty(timing2)) {
            timing2 = timing2.replace(":", "");
            byteBuf.writeBytes(CodeUtil.hexStringToByteArray(timing2));
        } else {
            byteBuf.writeShortLE(0x0000);
        }
        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * 表信息
     *
     * @param sn
     * @param codeCustomNB
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f6(int sn, int codeCustomNB, CommandParamDTO commandParamDTO) {
        String macAddr = commandParamDTO.getMacAddr();
        if (ComUtil.isEmpty(macAddr)) {
            log.error("f6: macAddr is null");
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(7);
        // 数据单元标识：FN
        byteBuf.writeByte(0x06);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 协议编码
        byteBuf.writeByte(codeCustomNB);
        // 表地址: 4字节
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(macAddr));
        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * NB温湿度记录仪温湿度修正
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f31(int sn, CommandParamDTO commandParamDTO) {
        Double correctTemperature = commandParamDTO.getCorrectTemperature();
        if (ComUtil.isEmpty(correctTemperature)) {
            log.error("f31: correctTemperature is null");
            return null;
        }

        Double correctHumidity = commandParamDTO.getCorrectHumidity();
        if (ComUtil.isEmpty(correctHumidity)) {
            log.error("f31: correctHumidity is null");
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(6);
        // 数据单元标识：FN
        byteBuf.writeByte(0x1F);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 温度修正值
        byteBuf.writeShortLE(BigDecimal.valueOf(correctTemperature).multiply(BigDecimal.valueOf(100)).shortValue());
        // 湿度修正值
        byteBuf.writeShortLE(BigDecimal.valueOf(correctHumidity).multiply(BigDecimal.valueOf(100)).shortValue());
        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * NBT200-AC温度修正
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f32(int sn, CommandParamDTO commandParamDTO) {
        Double correctTemperature = commandParamDTO.getCorrectTemperature();
        if (ComUtil.isEmpty(correctTemperature)) {
            log.error("f31: correctTemperature is null");
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(4);
        // 数据单元标识：FN
        byteBuf.writeByte(0x20);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 温度修正值
        byteBuf.writeShortLE(BigDecimal.valueOf(correctTemperature).multiply(BigDecimal.valueOf(100)).shortValue());
        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * NB温湿度记录仪全部信息1
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f250(int sn, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(50);
        // 数据单元标识：FN
        byteBuf.writeByte(0xFA);
        // 数据包序号
        byteBuf.writeByte(sn);

        // 上传模式 0：采后传  1：时段传    2：周期传
        Integer uploadMode = commandParamDTO.getUploadMode();
        if (ComUtil.isEmpty(uploadMode)) {
            log.error("f250: uploadMode is null");
            return null;
        }
        if (uploadMode < 0 || uploadMode > 2) {
            log.error("f250: uploadMode[{}] not in [0,2]", uploadMode);
            return null;
        }
        byteBuf.writeByte(uploadMode);

        // 上传时间
        if (uploadMode.compareTo(1) == 0) {
            String startTime = commandParamDTO.getStartTime();
            String endTime = commandParamDTO.getEndTime();
            if (ComUtil.isEmpty(startTime) || ComUtil.isEmpty(endTime)) {
                log.error("f250: uploadMode[{}], startTime[{}] or endTime[{}] is null", uploadMode, startTime, endTime);
                return null;
            }
            // 开始时间：HH:mm
            String[] startTimeArr = startTime.split(":");
            int startTimeHour = ComUtil.safeInt(startTimeArr[0]);
            if (startTimeHour < 0 || startTimeHour > 23) {
                log.error("f250: uploadMode[{}], startTimeHour[{}] not in [0,23]", uploadMode, startTimeHour);
                return null;
            }
            int startTimeMinute = ComUtil.safeInt(startTimeArr[1]);
            if (startTimeMinute < 0 || startTimeMinute > 59) {
                log.error("f250: uploadMode[{}], startTimeMinute[{}] not in [0,59]", uploadMode, startTimeMinute);
                return null;
            }
            byteBuf.writeByte(startTimeHour);
            byteBuf.writeByte(startTimeMinute);
            // 结束时间：HH:mm
            String[] endTimeArr = endTime.split(":");
            int endTimeHour = ComUtil.safeInt(endTimeArr[0]);
            if (endTimeHour < 0 || endTimeHour > 23) {
                log.error("f250: uploadMode[{}], endTimeHour[{}] not in [0,23]", uploadMode, endTimeHour);
                return null;
            }
            int endTimeMinute = ComUtil.safeInt(endTimeArr[1]);
            if (endTimeMinute < 0 || endTimeMinute > 59) {
                log.error("f250: uploadMode[{}], endTimeMinute[{}] not in [0,59]", uploadMode, endTimeMinute);
                return null;
            }
            byteBuf.writeByte(endTimeHour);
            byteBuf.writeByte(endTimeMinute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // 上传周期
        if (uploadMode.compareTo(2) == 0) {
            Integer uploadCycle = commandParamDTO.getUploadCycle();
            if (ComUtil.isEmpty(uploadCycle)) {
                log.error("f250: uploadMode[{}], uploadCycle is null", uploadMode);
                return null;
            }
            if (uploadCycle <= 0 || uploadCycle > 1440) {
                log.error("f250: uploadMode[{}], uploadCycle[{}] not in (0,1440]", uploadMode, uploadCycle);
                return null;
            }
            byteBuf.writeShortLE(uploadCycle);
        } else {
            byteBuf.writeShortLE(1440);
        }

        // 采集模式 0：周期  1：定时
        Integer collectMode = commandParamDTO.getCollectMode();
        if (ComUtil.isEmpty(collectMode)) {
            log.error("f250: collectMode is null");
            return null;
        }
        if (collectMode < 0 || collectMode > 1) {
            log.error("f250: collectMode[{}] not in [0,1]", collectMode);
            return null;
        }
        byteBuf.writeByte(collectMode);

        // 采集间隔
        if (collectMode.compareTo(0) == 0) {
            Integer collectCycle = commandParamDTO.getCollectCycle();
            if (ComUtil.isEmpty(collectCycle)) {
                log.error("f250: collectMode[{}], collectCycle is null", collectMode);
                return null;
            }
            if (collectCycle <= 0 || collectCycle > 1440) {
                log.error("f250: collectMode[{}], collectCycle[{}] not in (0,1440]", collectMode, collectCycle);
                return null;
            }
            byteBuf.writeShortLE(collectCycle);
        } else {
            byteBuf.writeShortLE(1440);
        }

        // 定时时间
        if (collectMode.compareTo(1) == 0) {
            String timing1 = commandParamDTO.getTiming1();
            String timing2 = commandParamDTO.getTiming2();
            if (ComUtil.isEmpty(timing1) || ComUtil.isEmpty(timing2)) {
                log.error("f250: collectMode[{}], timing1[{}] or timing2[{}] is null", collectMode, timing1, timing2);
                return null;
            }
            // 定时时间1：HH:mm
            String[] timing1Arr = timing1.split(":");
            int timing1Hour = ComUtil.safeInt(timing1Arr[0]);
            if (timing1Hour < 0 || timing1Hour > 23) {
                log.error("f250: collectMode[{}], timing1Hour[{}] not in [0,23]", collectMode, timing1Hour);
                return null;
            }
            int timing1Minute = ComUtil.safeInt(timing1Arr[1]);
            if (timing1Minute < 0 || timing1Minute > 59) {
                log.error("f250: collectMode[{}], timing1Minute[{}] not in [0,59]", collectMode, timing1Minute);
                return null;
            }
            byteBuf.writeByte(timing1Hour);
            byteBuf.writeByte(timing1Minute);

            // 定时时间2：HH:mm
            String[] timing2Arr = timing1.split(":");
            int timing2Hour = ComUtil.safeInt(timing2Arr[0]);
            if (timing2Hour < 0 || timing2Hour > 23) {
                log.error("f250: collectMode[{}], timing2Hour[{}] not in [0,23]", collectMode, timing2Hour);
                return null;
            }
            int timing2Minute = ComUtil.safeInt(timing2Arr[1]);
            if (timing2Minute < 0 || timing2Minute > 59) {
                log.error("f250: collectMode[{}], timing2Minute[{}] not in [0,59]", collectMode, timing2Minute);
                return null;
            }
            byteBuf.writeByte(timing2Hour);
            byteBuf.writeByte(timing2Minute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // ip或域名
        String ip = commandParamDTO.getIp();
        if (ComUtil.isEmpty(ip)) {
            log.error("f250: ip is null");
            return null;
        }
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(ip)));
        for (int i = 0, len = Constant.BYTE_LENGTH_IP - ip.length(); i < len; i++) {
            byteBuf.writeByte(0x00);
        }

        // 端口
        Integer port = commandParamDTO.getPort();
        if (ComUtil.isEmpty(port)) {
            log.error("f250: port is null");
            return null;
        }
        byteBuf.writeShortLE(port);

        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * NB温湿度记录仪全部信息2
     *
     * @param sn
     * @param codeCustomNB
     * @param macAddr
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f251(int sn, int codeCustomNB, String macAddr, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(55);
        // 数据单元标识：FN
        byteBuf.writeByte(0xFB);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 协议编码
        byteBuf.writeByte(codeCustomNB);
        // 表地址: 4字节
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(macAddr));

        // 上传模式 0：采后传  1：时段传    2：周期传
        Integer uploadMode = commandParamDTO.getUploadMode();
        if (ComUtil.isEmpty(uploadMode)) {
            log.error("f251: uploadMode is null");
            return null;
        }
        if (uploadMode < 0 || uploadMode > 2) {
            log.error("f251: uploadMode[{}] not in [0,2]", uploadMode);
            return null;
        }
        byteBuf.writeByte(uploadMode);

        // 上传时间
        if (uploadMode.compareTo(1) == 0) {
            String startTime = commandParamDTO.getStartTime();
            String endTime = commandParamDTO.getEndTime();
            if (ComUtil.isEmpty(startTime) || ComUtil.isEmpty(endTime)) {
                log.error("f251: uploadMode[{}], startTime[{}] or endTime[{}] is null", uploadMode, startTime, endTime);
                return null;
            }
            // 开始时间：HH:mm
            String[] startTimeArr = startTime.split(":");
            int startTimeHour = ComUtil.safeInt(startTimeArr[0]);
            if (startTimeHour < 0 || startTimeHour > 23) {
                log.error("f251: uploadMode[{}], startTimeHour[{}] not in [0,23]", uploadMode, startTimeHour);
                return null;
            }
            int startTimeMinute = ComUtil.safeInt(startTimeArr[1]);
            if (startTimeMinute < 0 || startTimeMinute > 59) {
                log.error("f251: uploadMode[{}], startTimeMinute[{}] not in [0,59]", uploadMode, startTimeMinute);
                return null;
            }
            byteBuf.writeByte(startTimeHour);
            byteBuf.writeByte(startTimeMinute);
            // 结束时间：HH:mm
            String[] endTimeArr = endTime.split(":");
            int endTimeHour = ComUtil.safeInt(endTimeArr[0]);
            if (endTimeHour < 0 || endTimeHour > 23) {
                log.error("f251: uploadMode[{}], endTimeHour[{}] not in [0,23]", uploadMode, endTimeHour);
                return null;
            }
            int endTimeMinute = ComUtil.safeInt(endTimeArr[1]);
            if (endTimeMinute < 0 || endTimeMinute > 59) {
                log.error("f251: uploadMode[{}], endTimeMinute[{}] not in [0,59]", uploadMode, endTimeMinute);
                return null;
            }
            byteBuf.writeByte(endTimeHour);
            byteBuf.writeByte(endTimeMinute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // 上传周期
        if (uploadMode.compareTo(2) == 0) {
            Integer uploadCycle = commandParamDTO.getUploadCycle();
            if (ComUtil.isEmpty(uploadCycle)) {
                log.error("f251: uploadMode[{}], uploadCycle is null", uploadMode);
                return null;
            }
            if (uploadCycle <= 0 || uploadCycle > 1440) {
                log.error("f251: uploadMode[{}], uploadCycle[{}] not in (0,1440]", uploadMode, uploadCycle);
                return null;
            }
            byteBuf.writeShortLE(uploadCycle);
        } else {
            byteBuf.writeShortLE(1440);
        }

        // 采集模式 0：周期  1：定时
        Integer collectMode = commandParamDTO.getCollectMode();
        if (ComUtil.isEmpty(collectMode)) {
            log.error("f251: collectMode is null");
            return null;
        }
        if (collectMode < 0 || collectMode > 1) {
            log.error("f251: collectMode[{}] not in [0,1]", collectMode);
            return null;
        }
        byteBuf.writeByte(collectMode);

        // 采集间隔
        if (collectMode.compareTo(0) == 0) {
            Integer collectCycle = commandParamDTO.getCollectCycle();
            if (ComUtil.isEmpty(collectCycle)) {
                log.error("f251: collectMode[{}], collectCycle is null", collectMode);
                return null;
            }
            if (collectCycle <= 0 || collectCycle > 1440) {
                log.error("f251: collectMode[{}], collectCycle[{}] not in (0,1440]", collectMode, collectCycle);
                return null;
            }
            byteBuf.writeShortLE(collectCycle);
        } else {
            byteBuf.writeShortLE(1440);
        }

        // 定时时间
        if (collectMode.compareTo(1) == 0) {
            String timing1 = commandParamDTO.getTiming1();
            String timing2 = commandParamDTO.getTiming2();
            if (ComUtil.isEmpty(timing1) || ComUtil.isEmpty(timing2)) {
                log.error("f251: collectMode[{}], timing1[{}] or timing2[{}] is null", collectMode, timing1, timing2);
                return null;
            }
            // 定时时间1：HH:mm
            String[] timing1Arr = timing1.split(":");
            int timing1Hour = ComUtil.safeInt(timing1Arr[0]);
            if (timing1Hour < 0 || timing1Hour > 23) {
                log.error("f251: collectMode[{}], timing1Hour[{}] not in [0,23]", collectMode, timing1Hour);
                return null;
            }
            int timing1Minute = ComUtil.safeInt(timing1Arr[1]);
            if (timing1Minute < 0 || timing1Minute > 59) {
                log.error("f251: collectMode[{}], timing1Minute[{}] not in [0,59]", collectMode, timing1Minute);
                return null;
            }
            byteBuf.writeByte(timing1Hour);
            byteBuf.writeByte(timing1Minute);

            // 定时时间2：HH:mm
            String[] timing2Arr = timing1.split(":");
            int timing2Hour = ComUtil.safeInt(timing2Arr[0]);
            if (timing2Hour < 0 || timing2Hour > 23) {
                log.error("f251: collectMode[{}], timing2Hour[{}] not in [0,23]", collectMode, timing2Hour);
                return null;
            }
            int timing2Minute = ComUtil.safeInt(timing2Arr[1]);
            if (timing2Minute < 0 || timing2Minute > 59) {
                log.error("f251: collectMode[{}], timing2Minute[{}] not in [0,59]", collectMode, timing2Minute);
                return null;
            }
            byteBuf.writeByte(timing2Hour);
            byteBuf.writeByte(timing2Minute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // ip或域名
        String ip = commandParamDTO.getIp();
        if (ComUtil.isEmpty(ip)) {
            log.error("f251: ip is null");
            return null;
        }
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(ip)));
        for (int i = 0, len = Constant.BYTE_LENGTH_IP - ip.length(); i < len; i++) {
            byteBuf.writeByte(0x00);
        }

        // 端口
        Integer port = commandParamDTO.getPort();
        if (ComUtil.isEmpty(port)) {
            log.error("f251: port is null");
            return null;
        }
        byteBuf.writeShortLE(port);

        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * 全部信息1
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f254(int sn, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(48);
        // 数据单元标识：FN
        byteBuf.writeByte(0xFE);
        // 数据包序号
        byteBuf.writeByte(sn);

        // 上传模式 0：采后传  1：时段传
        Integer uploadMode = commandParamDTO.getUploadMode();
        if (ComUtil.isEmpty(uploadMode)) {
            log.error("f254: uploadMode is null");
            return null;
        }
        if (uploadMode < 0 || uploadMode > 1) {
            log.error("f254: uploadMode[{}] not in [0,1]", uploadMode);
            return null;
        }
        byteBuf.writeByte(uploadMode);

        // 上传时间
        if (uploadMode.compareTo(1) == 0) {
            String startTime = commandParamDTO.getStartTime();
            String endTime = commandParamDTO.getEndTime();
            if (ComUtil.isEmpty(startTime) || ComUtil.isEmpty(endTime)) {
                log.error("f254: uploadMode[{}], startTime[{}] or endTime[{}] is null", uploadMode, startTime, endTime);
                return null;
            }
            // 开始时间：HH:mm
            String[] startTimeArr = startTime.split(":");
            int startTimeHour = ComUtil.safeInt(startTimeArr[0]);
            if (startTimeHour < 0 || startTimeHour > 23) {
                log.error("f254: uploadMode[{}], startTimeHour[{}] not in [0,23]", uploadMode, startTimeHour);
                return null;
            }
            int startTimeMinute = ComUtil.safeInt(startTimeArr[1]);
            if (startTimeMinute < 0 || startTimeMinute > 59) {
                log.error("f254: uploadMode[{}], startTimeMinute[{}] not in [0,59]", uploadMode, startTimeMinute);
                return null;
            }
            byteBuf.writeByte(startTimeHour);
            byteBuf.writeByte(startTimeMinute);
            // 结束时间：HH:mm
            String[] endTimeArr = endTime.split(":");
            int endTimeHour = ComUtil.safeInt(endTimeArr[0]);
            if (endTimeHour < 0 || endTimeHour > 23) {
                log.error("f254: uploadMode[{}], endTimeHour[{}] not in [0,23]", uploadMode, endTimeHour);
                return null;
            }
            int endTimeMinute = ComUtil.safeInt(endTimeArr[1]);
            if (endTimeMinute < 0 || endTimeMinute > 59) {
                log.error("f254: uploadMode[{}], endTimeMinute[{}] not in [0,59]", uploadMode, endTimeMinute);
                return null;
            }
            byteBuf.writeByte(endTimeHour);
            byteBuf.writeByte(endTimeMinute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // 采集模式 0：周期  1：定时
        Integer collectMode = commandParamDTO.getCollectMode();
        if (ComUtil.isEmpty(collectMode)) {
            log.error("f254: collectMode is null");
            return null;
        }
        if (collectMode < 0 || collectMode > 1) {
            log.error("f254: collectMode[{}] not in [0,1]", collectMode);
            return null;
        }
        byteBuf.writeByte(collectMode);

        // 采集间隔
        if (collectMode.compareTo(0) == 0) {
            Integer collectCycle = commandParamDTO.getCollectCycle();
            if (ComUtil.isEmpty(collectCycle)) {
                log.error("f254: collectMode[{}], collectCycle is null", collectMode);
                return null;
            }
            if (collectCycle <= 0 || collectCycle > 1440) {
                log.error("f254: collectMode[{}], collectCycle[{}] not in (0,1440]", collectMode, collectCycle);
                return null;
            }
            byteBuf.writeShortLE(collectCycle);
        } else {
            byteBuf.writeShortLE(1440);
        }

        // 定时时间
        if (collectMode.compareTo(1) == 0) {
            String timing1 = commandParamDTO.getTiming1();
            String timing2 = commandParamDTO.getTiming2();
            if (ComUtil.isEmpty(timing1) || ComUtil.isEmpty(timing2)) {
                log.error("f254: collectMode[{}], timing1[{}] or timing2[{}] is null", collectMode, timing1, timing2);
                return null;
            }
            // 定时时间1：HH:mm
            String[] timing1Arr = timing1.split(":");
            int timing1Hour = ComUtil.safeInt(timing1Arr[0]);
            if (timing1Hour < 0 || timing1Hour > 23) {
                log.error("f254: collectMode[{}], timing1Hour[{}] not in [0,23]", collectMode, timing1Hour);
                return null;
            }
            int timing1Minute = ComUtil.safeInt(timing1Arr[1]);
            if (timing1Minute < 0 || timing1Minute > 59) {
                log.error("f254: collectMode[{}], timing1Minute[{}] not in [0,59]", collectMode, timing1Minute);
                return null;
            }
            byteBuf.writeByte(timing1Hour);
            byteBuf.writeByte(timing1Minute);

            // 定时时间2：HH:mm
            String[] timing2Arr = timing1.split(":");
            int timing2Hour = ComUtil.safeInt(timing2Arr[0]);
            if (timing2Hour < 0 || timing2Hour > 23) {
                log.error("f254: collectMode[{}], timing2Hour[{}] not in [0,23]", collectMode, timing2Hour);
                return null;
            }
            int timing2Minute = ComUtil.safeInt(timing2Arr[1]);
            if (timing2Minute < 0 || timing2Minute > 59) {
                log.error("f254: collectMode[{}], timing2Minute[{}] not in [0,59]", collectMode, timing2Minute);
                return null;
            }
            byteBuf.writeByte(timing2Hour);
            byteBuf.writeByte(timing2Minute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // ip或域名
        String ip = commandParamDTO.getIp();
        if (ComUtil.isEmpty(ip)) {
            log.error("f254: ip is null");
            return null;
        }
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(ip)));
        for (int i = 0, len = Constant.BYTE_LENGTH_IP - ip.length(); i < len; i++) {
            byteBuf.writeByte(0x00);
        }

        // 端口
        Integer port = commandParamDTO.getPort();
        if (ComUtil.isEmpty(port)) {
            log.error("f254: port is null");
            return null;
        }
        byteBuf.writeShortLE(port);

        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * 全部信息2
     *
     * @param sn
     * @param codeCustomNB
     * @param macAddr
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f255(int sn, int codeCustomNB, String macAddr, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(53);
        // 数据单元标识：FN
        byteBuf.writeByte(0xFF);
        // 数据包序号
        byteBuf.writeByte(sn);
        // 协议编码
        byteBuf.writeByte(codeCustomNB);
        // 表地址: 4字节
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(macAddr));

        // 上传模式 0：采后传  1：时段传
        Integer uploadMode = commandParamDTO.getUploadMode();
        if (ComUtil.isEmpty(uploadMode)) {
            log.error("f255: uploadMode is null");
            return null;
        }
        if (uploadMode < 0 || uploadMode > 1) {
            log.error("f255: uploadMode[{}] not in [0,1]", uploadMode);
            return null;
        }
        byteBuf.writeByte(uploadMode);

        // 上传时间
        if (uploadMode.compareTo(1) == 0) {
            String startTime = commandParamDTO.getStartTime();
            String endTime = commandParamDTO.getEndTime();
            if (ComUtil.isEmpty(startTime) || ComUtil.isEmpty(endTime)) {
                log.error("f255: uploadMode[{}], startTime[{}] or endTime[{}] is null", uploadMode, startTime, endTime);
                return null;
            }
            // 开始时间：HH:mm
            String[] startTimeArr = startTime.split(":");
            int startTimeHour = ComUtil.safeInt(startTimeArr[0]);
            if (startTimeHour < 0 || startTimeHour > 23) {
                log.error("f255: uploadMode[{}], startTimeHour[{}] not in [0,23]", uploadMode, startTimeHour);
                return null;
            }
            int startTimeMinute = ComUtil.safeInt(startTimeArr[1]);
            if (startTimeMinute < 0 || startTimeMinute > 59) {
                log.error("f255: uploadMode[{}], startTimeMinute[{}] not in [0,59]", uploadMode, startTimeMinute);
                return null;
            }
            byteBuf.writeByte(startTimeHour);
            byteBuf.writeByte(startTimeMinute);
            // 结束时间：HH:mm
            String[] endTimeArr = endTime.split(":");
            int endTimeHour = ComUtil.safeInt(endTimeArr[0]);
            if (endTimeHour < 0 || endTimeHour > 23) {
                log.error("f255: uploadMode[{}], endTimeHour[{}] not in [0,23]", uploadMode, endTimeHour);
                return null;
            }
            int endTimeMinute = ComUtil.safeInt(endTimeArr[1]);
            if (endTimeMinute < 0 || endTimeMinute > 59) {
                log.error("f255: uploadMode[{}], endTimeMinute[{}] not in [0,59]", uploadMode, endTimeMinute);
                return null;
            }
            byteBuf.writeByte(endTimeHour);
            byteBuf.writeByte(endTimeMinute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // 采集模式 0：周期  1：定时
        Integer collectMode = commandParamDTO.getCollectMode();
        if (ComUtil.isEmpty(collectMode)) {
            log.error("f255: collectMode is null");
            return null;
        }
        if (collectMode < 0 || collectMode > 1) {
            log.error("f255: collectMode[{}] not in [0,1]", collectMode);
            return null;
        }
        byteBuf.writeByte(collectMode);

        // 采集间隔
        if (collectMode.compareTo(0) == 0) {
            Integer collectCycle = commandParamDTO.getCollectCycle();
            if (ComUtil.isEmpty(collectCycle)) {
                log.error("f255: collectMode[{}], collectCycle is null", collectMode);
                return null;
            }
            if (collectCycle <= 0 || collectCycle > 1440) {
                log.error("f255: collectMode[{}], collectCycle[{}] not in (0,1440]", collectMode, collectCycle);
                return null;
            }
            byteBuf.writeShortLE(collectCycle);
        } else {
            byteBuf.writeShortLE(1440);
        }

        // 定时时间
        if (collectMode.compareTo(1) == 0) {
            String timing1 = commandParamDTO.getTiming1();
            String timing2 = commandParamDTO.getTiming2();
            if (ComUtil.isEmpty(timing1) || ComUtil.isEmpty(timing2)) {
                log.error("f255: collectMode[{}], timing1[{}] or timing2[{}] is null", collectMode, timing1, timing2);
                return null;
            }
            // 定时时间1：HH:mm
            String[] timing1Arr = timing1.split(":");
            int timing1Hour = ComUtil.safeInt(timing1Arr[0]);
            if (timing1Hour < 0 || timing1Hour > 23) {
                log.error("f255: collectMode[{}], timing1Hour[{}] not in [0,23]", collectMode, timing1Hour);
                return null;
            }
            int timing1Minute = ComUtil.safeInt(timing1Arr[1]);
            if (timing1Minute < 0 || timing1Minute > 59) {
                log.error("f255: collectMode[{}], timing1Minute[{}] not in [0,59]", collectMode, timing1Minute);
                return null;
            }
            byteBuf.writeByte(timing1Hour);
            byteBuf.writeByte(timing1Minute);

            // 定时时间2：HH:mm
            String[] timing2Arr = timing1.split(":");
            int timing2Hour = ComUtil.safeInt(timing2Arr[0]);
            if (timing2Hour < 0 || timing2Hour > 23) {
                log.error("f255: collectMode[{}], timing2Hour[{}] not in [0,23]", collectMode, timing2Hour);
                return null;
            }
            int timing2Minute = ComUtil.safeInt(timing2Arr[1]);
            if (timing2Minute < 0 || timing2Minute > 59) {
                log.error("f255: collectMode[{}], timing2Minute[{}] not in [0,59]", collectMode, timing2Minute);
                return null;
            }
            byteBuf.writeByte(timing2Hour);
            byteBuf.writeByte(timing2Minute);
        } else {
            byteBuf.writeShortLE(0x0000);
            byteBuf.writeShortLE(0x0000);
        }

        // ip或域名
        String ip = commandParamDTO.getIp();
        if (ComUtil.isEmpty(ip)) {
            log.error("f255: ip is null");
            return null;
        }
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(ip)));
        for (int i = 0, len = Constant.BYTE_LENGTH_IP - ip.length(); i < len; i++) {
            byteBuf.writeByte(0x00);
        }

        // 端口
        Integer port = commandParamDTO.getPort();
        if (ComUtil.isEmpty(port)) {
            log.error("f255: port is null");
            return null;
        }
        byteBuf.writeShortLE(port);

        return Unpooled.wrappedBuffer(packDefault(), byteBuf);
    }

    /**
     * 命令包：默认
     *
     * @return
     */
    private static ByteBuf packDefault() {
        ByteBuf byteBuf = Unpooled.buffer(2);
        // 功能码：AFN
        byteBuf.writeByte(0x03);
        // 帧序列：SEQ，默认FF
        byteBuf.writeByte(0xFF);
        return byteBuf;
    }
}
